<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /* 
          a b c d
      r = a+d+c+b
      */
      function exchange(str, x, y) {
        // x=b,y=d
        let r = "";
        for (let v of str) {
          if (v === x) {
            r += y;
          } else if (v === y) {
            r += x;
          } else {
            r += v;
          }
        }
        return r;
      }
      console.log(exchange("abcd", "b", "d"));
    </script>
  </body>
</html>
